from PIL import Image
import os
import glob
from tqdm import tqdm  # Import tqdm for the progress bar


def resize_images_in_folder(folder_path, size=(512, 512)):
    # Finding all jpg files in the folder
    jpg_files = glob.glob(os.path.join(folder_path, '*.jpg'))

    for file in tqdm(jpg_files, desc="Resizing images"):
        try:
            img = Image.open(file)
            img = img.resize(size, Image.ANTIALIAS)
            img.save(file)  # Overwrite the original image
        except Exception as e:
            print(f"Error resizing image {file}: {e}")

    return len(jpg_files)


folder_path = './6_images'
number_of_images_resized = resize_images_in_folder(folder_path)
print(f"Number of images resized: {number_of_images_resized}")
